/*     */ package com.java.diagnostics.visualizer.impl.sources;
/*     */ 
/*     */ import com.java.diagnostics.visualizer.exceptions.GCAndMemoryVisualizerException;
/*     */ import com.java.diagnostics.visualizer.impl.factory.LogFactory;
/*     */ import com.java.diagnostics.visualizer.sources.Source;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PushbackInputStream;
/*     */ import java.nio.charset.Charset;
/*     */ import java.nio.charset.CharsetDecoder;
/*     */ import java.nio.charset.CodingErrorAction;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import java.util.TreeSet;
/*     */ import java.util.logging.Logger;
/*     */ 
/*     */ public abstract class SourceImpl
/*     */   implements Source
/*     */ {
/*  36 */   private static final Logger TRACE = LogFactory.getTrace(SourceImpl.class);
/*     */   private static final int MAX_PEEK_BYTES = 1000;
/*     */   private static final String EBCDIC_CHARSET_NAME = "ibm-1047";
/*     */   private Charset lastCharset;
/*     */   private String variant;
/*  45 */   protected SourcePreferenceHelper helper = new SourcePreferenceHelper();
/*     */   private String name;
/*  49 */   private Set specificContentTypes = new HashSet();
/*     */ 
/*  51 */   private Set generalContentTypes = new HashSet();
/*     */   protected PositionAwareBufferedInputStream stream;
/*     */   private List<Integer> positions;
/*     */   private static final int UNSET = -1;
/*  59 */   protected static final String NEWLINE = System.getProperty("line.separator");
/*     */ 
/*  63 */   private static final Set<String> variantNames = new TreeSet();
/*  64 */   private static final Set<String> names = new TreeSet();
/*     */ 
/*     */   protected SourceImpl(String name) {
/*  67 */     this.name = ensureUniqueName(name, names);
/*     */ 
/*  71 */     int i = name.lastIndexOf("/");
/*  72 */     if (i != -1)
/*  73 */       i = name.lastIndexOf("\"");
/*     */     String v;
/*     *          String v;
/*  76 */     if (i != -1)
/*  77 */       v = name.substring(i, name.length());
/*     */     else {
/*  79 */       v = name;
/*     */     }
/*  81 */     this.variant = ensureUniqueName(v, variantNames);
/*  82 */     variantNames.add(this.variant);
/*  83 */     this.positions = new ArrayList();
/*     */   }
/*     */ 
/*     */   private String ensureUniqueName(String originalName, Set allNames)
/*     */   {
/*  88 */     String aName = originalName;
/*  89 */     int count = 2;
/*  90 */     while (variantNames.contains(aName))
/*     */     {
/*  92 */       aName = originalName + " (" + count + ")";
/*  93 */       count++;
/*     */     }
/*  95 */     return aName;
/*     */   }
/*     */ 
/*     */   public String getName() {
/*  99 */     return this.name;
/*     */   }
/*     */ 
/*     */   public String getVariantIdentifier()
/*     */   {
/* 109 */     return this.variant;
/*     */   }
/*     */ 
/*     */   public void addGeneralContentType(String contentType) {
/* 113 */     this.generalContentTypes.add(contentType);
/*     */   }
/*     */ 
/*     */   public void addSpecificContentType(String contentType)
/*     */   {
/* 118 */     this.specificContentTypes.add(contentType);
/*     */   }
/*     */ 
/*     */   public Set getGeneralContentTypes() {
/* 122 */     return this.generalContentTypes;
/*     */   }
/*     */ 
/*     */   public Set getSpecificContentTypes() {
/* 126 */     return this.specificContentTypes;
/*     */   }
/*     */ 
/*     */   public void newSequenceUID(int sequenceUID) {
/* 130 */     if (this.stream != null) {
/* 131 */       int offset = this.stream.getPosition();
/* 132 */       this.positions.add(Integer.valueOf(offset));
/*     */     }
/*     */   }
/*     */ 
/*     */   public void resetPositions()
/*     */   {
/* 142 */     this.positions = new ArrayList();
/*     */   }
/*     */ 
/*     */   public InputStream getStream() throws GCAndMemoryVisualizerException
/*     */   {
/* 147 */     this.stream = new PositionAwareBufferedInputStream(getRawStream());
/* 148 */     return this.stream;
/*     */   }
/*     */ 
/*     */   protected abstract InputStream getRawStream()
/*     */     throws GCAndMemoryVisualizerException;
/*     */ 
/*     */   public BufferedReader getSafeBufferedStreamReader()
/*     */     throws GCAndMemoryVisualizerException
/*     */   {
/*     */     InputStream inStream;
/*     */     Charset charset;
/*     *          InputStream inStream;
/* 172 */     if ((this.lastCharset != null) && (!hasChangedSinceLastGet())) {
/* 173 */        charset = this.lastCharset;
/* 174 */       inStream = getStream();
/*     */     } else {
/* 176 */       PushbackInputStream pushbackInStream = new PushbackInputStream(getStream(), 1000);
/* 177 */       charset = getCharset(pushbackInStream, 1000);
/* 178 */       inStream = pushbackInStream;
/*     */     }
/* 180 */     CharsetDecoder defaultDecoder = charset.newDecoder();
/* 181 */     defaultDecoder.onMalformedInput(CodingErrorAction.IGNORE);
/* 182 */     defaultDecoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
/* 183 */     return new BufferedReader(new InputStreamReader(inStream, defaultDecoder));
/*     */   }
/*     */ 
/*     */   private Charset getCharset(PushbackInputStream inStream, int bytesToScan)
/*     */   {
/* 191 */     Charset charset = Charset.defaultCharset();
/* 192 */     byte[] peekBytes = new byte[bytesToScan];
/* 193 */     int peekedBytes = -1;
/*     */     try {
/* 195 */       peekedBytes = inStream.read(peekBytes);
/* 196 */       if (peekedBytes > 0) {
/* 197 */         for (int i = 0; i < peekedBytes; i++) {
/* 198 */           byte b = peekBytes[i];
/*     */ 
/* 200 */           if (b == 21) {
/* 201 */             charset = Charset.forName("ibm-1047");
/*     */           }
/*     */           else {
/* 204 */             if ((b == 10) || 
/* 205 */               (b == 58) || 
/* 206 */               (b == 60) || 
/* 207 */               (b == 61))
/*     */               break;
/*     */           }
/*     */         }
/* 211 */         inStream.unread(peekBytes, 0, peekedBytes);
/*     */       }
/*     */     }
/*     */     catch (Exception localException) {
/* 215 */       this.lastCharset = null;
/*     */ 
/* 217 */       return Charset.defaultCharset();
/*     */     }
/* 219 */     this.lastCharset = charset;
/* 220 */     return charset;
/*     */   }
/*     */ 
/*     */   public int getOffset(int sequenceUID) {
/* 224 */     if ((this.positions != null) && (sequenceUID >= 0) && 
/* 225 */       (this.positions.size() > sequenceUID)) {
/* 226 */       int position = ((Integer)this.positions.get(sequenceUID)).intValue();
/* 227 */       TRACE.warning("SequenceUID " + sequenceUID + " maps to: " + position);
/* 228 */       return position;
/*     */     }
/* 230 */     return -1;
/*     */   }
/*     */ 
/*     */   public int getSequenceUID(int offset)
/*     */   {
/* 235 */     if (this.positions != null) {
/* 236 */       int index = Collections.binarySearch(this.positions, Integer.valueOf(offset));
/*     */ 
/* 240 */       if (index < 0) {
/* 241 */         index++;
/* 242 */         index = Math.abs(index);
/*     */       }
/* 244 */       if (index >= this.positions.size()) {
/* 245 */         index = this.positions.size() - 1;
/*     */       }
/* 247 */       return index;
/*     */     }
/* 249 */     return -1;
/*     */   }
/*     */ }

